package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityXfDeviceMealConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.payconfig.ConsDeviceMealConfigVo;
import com.ocom.common.request.payconfig.SetConsDeviceMealConfigRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfDeviceMealConfig;
import com.ocom.payconfig.service.XfDeviceMealConfigService;
import com.ocom.redis.util.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class XfDeviceMealConfigServiceImpl extends ServiceImpl<IDaoXfDeviceMealConfig, EntityXfDeviceMealConfig> implements XfDeviceMealConfigService {

	@Resource
	RedisUtil redisUtil;

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
	public List<EntityXfDeviceMealConfig> getConsDeviceMealConfig(Long comId, String deviceNo, Integer consType) {
		QueryWrapper<EntityXfDeviceMealConfig> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id",comId);
		queryWrapper.eq("device_no",deviceNo);
		queryWrapper.eq("cons_type",consType);
		return this.list(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
	public Boolean setConsDeviceMealConfig(Long comId,SetConsDeviceMealConfigRequest request) {


		if(request.getConfigList()==null || request.getConfigList().size()==0){
			QueryWrapper<EntityXfDeviceMealConfig> queryWrapper=new QueryWrapper<>();
			queryWrapper.eq("com_id",request.getComId());
			queryWrapper.eq("device_no",request.getDeviceNo());
			this.remove(queryWrapper);
			return true;
		}
//		QueryWrapper<EntityXfDeviceMealConfig> queryWrapper1=new QueryWrapper<>();
//		queryWrapper1.eq("com_id",comId);
//		queryWrapper1.eq("device_no",request.getDeviceNo());
//		queryWrapper1.eq("cons_type",request.getConsType());
//		List<EntityXfDeviceMealConfig> oldList =  this.list(queryWrapper1);
//		List<Integer> mealList =new ArrayList<>();
//		if(oldList!=null && oldList.size()>0){
//			mealList = oldList.stream().map(EntityXfDeviceMealConfig -> EntityXfDeviceMealConfig.getMealNo()).collect(Collectors.toList());
//		}
//		redisUtil.del("xf_device_info#comId:"+request.getComId()+"#machNo:"+request.getDeviceNo()+"#");
//		redisUtil.del("xf_device_meal_config#comId:"+request.getComId()+"#machNo:"+request.getDeviceNo()+"#");

		for (ConsDeviceMealConfigVo consDeviceMealConfigVo:request.getConfigList()){
			UpdateWrapper<EntityXfDeviceMealConfig> updateWrapper = Wrappers.update();
			updateWrapper.eq("com_id",request.getComId());
			updateWrapper.eq("device_no",request.getDeviceNo());
			updateWrapper.eq("cons_type",request.getConsType());
			updateWrapper.eq("meal_no",consDeviceMealConfigVo.getMealNo());

			EntityXfDeviceMealConfig entityXfDeviceMealConfig =new EntityXfDeviceMealConfig();
			entityXfDeviceMealConfig.setComId(request.getComId());
			entityXfDeviceMealConfig.setDeviceNo(request.getDeviceNo());
			entityXfDeviceMealConfig.setConsType(request.getConsType());
			entityXfDeviceMealConfig.setMealNo(consDeviceMealConfigVo.getMealNo());
			entityXfDeviceMealConfig.setMealMoney(consDeviceMealConfigVo.getMealMoney());
			entityXfDeviceMealConfig.setKeyMode(consDeviceMealConfigVo.getKeyMode());
			entityXfDeviceMealConfig.setAccountIds(consDeviceMealConfigVo.getAccounts());
			entityXfDeviceMealConfig.setVoucherMode(consDeviceMealConfigVo.getVoucherMode());
			entityXfDeviceMealConfig.setVoucherTypes(consDeviceMealConfigVo.getVoucherTypes());
			this.saveOrUpdate(entityXfDeviceMealConfig,updateWrapper);
		}
		redisUtil.deleteWithRetry("xf_device_info#comId:"+request.getComId()+"#machNo:"+request.getDeviceNo()+"#");
		redisUtil.deleteWithRetry("xf_device_meal_config#comId:"+request.getComId()+"#machNo:"+request.getDeviceNo()+"#");
		return true;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
	public Boolean delConsDeviceMealConfig(Long comId, String deviceNo) {
		QueryWrapper<EntityXfDeviceMealConfig> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id",comId);
		queryWrapper.eq("device_no",deviceNo);
//		List<EntityXfDeviceMealConfig>  list = this.list(queryWrapper);
//		if(list==null) return true;
//		for (EntityXfDeviceMealConfig entityXfDeviceMealConfig:list){
//			redisUtil.del("xf_device_meal_config#comId:"+comId+"#machNo:"+deviceNo+"#mealNo:"+entityXfDeviceMealConfig.getMealNo()+"#");
//		}
		redisUtil.del("xf_device_meal_config#comId:"+comId+"#machNo:"+deviceNo+"#");
		return this.remove(queryWrapper);
	}
}
